using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VSSDKAssist.Assets.CustomPages;
using Microsoft.Practices.WizardFramework;
using EnvDTE;
using VSSDKAssist.Assets.Editors;
using System.Collections.Specialized;
using Microsoft.Practices.RecipeFramework.Library.Converters;
using Microsoft.Practices.RecipeFramework.Library.Editors;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CreateVSToolWindow
{
	/// <summary>
	/// Custom page for CreateVSToolWindow recipe
	/// </summary>
	public partial class CreateVSToolWindowCP2 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSToolWindowCP2"/> class.
		/// </summary>
		public CreateVSToolWindowCP2()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSToolWindowCP2"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public CreateVSToolWindowCP2(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
			SetControls();
		}

		/// <summary>
		/// Gets or sets the tool window caption.
		/// </summary>
		/// <value>The tool window caption.</value>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value"), RecipeArgument]
		public string ToolWindowCaption
		{
			get { return (string)veToolWindowCaption.Value; }
			set
			{
			}
		}

		/// <summary>
		/// Gets or sets the bitmap destination folder.
		/// </summary>
		/// <value>The bitmap destination folder.</value>
		[RecipeArgument]
		public ProjectItem BitmapDestinationFolder
		{
			get { return (ProjectItem)veResourcesFolder.Value; }
			set
			{
				if(value != null)
				{					
					veResourcesFolder.TextBox.Text = new ProjectItemConverter().ConvertTo(value, typeof(string)) as string;
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the bitmap file.
		/// </summary>
		/// <value>The name of the bitmap file.</value>
		[RecipeArgument]
		public string BitmapFileName
		{
			get { return (string)veToolWindowIcon.Value; }
			set
			{
				if(value != null)
				{
					veToolWindowIcon.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the RESX project item.
		/// </summary>
		/// <value>The RESX project item.</value>
		[RecipeArgument]
		public ProjectItem ResxProjectItem
		{
			get { return (ProjectItem)veResxProjectItem.Value; }
			set
			{
				if(value != null)
				{
					veResxProjectItem.TextBox.Text = new ProjectItemConverter().ConvertTo(value, typeof(string)) as string;
				}
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veToolWindowCaption.BeginInit();
					this.veToolWindowCaption.ValueType = typeof(string);
					base.Site.Container.Add(veToolWindowCaption);
					this.veToolWindowCaption.EndInit();

					this.veResourcesFolder.BeginInit();
					this.veResourcesFolder.ValueType = typeof(ProjectItem);
					base.Site.Container.Add(veResourcesFolder);
					SolutionPickerEditor solutionPickerEditor1 = new SolutionPickerEditor();
					StringDictionary solutionPickerEditorAttributes1 = new StringDictionary();
					solutionPickerEditorAttributes1.Add("UnboundReferenceType", "VSSDKAssist.Assets.References.VisualStudio.ProjectFolderReference, VSSDKAssist.Assets");
					solutionPickerEditor1.Configure(solutionPickerEditorAttributes1);
					this.veResourcesFolder.EditorInstance = solutionPickerEditor1;
					this.veResourcesFolder.ConverterInstance = new ProjectItemConverter();
					this.veResourcesFolder.EndInit();

					this.veResxProjectItem.BeginInit();
					this.veResxProjectItem.ValueType = typeof(ProjectItem);
					base.Site.Container.Add(veResxProjectItem);
					SolutionPickerEditor solutionPickerEditor = new SolutionPickerEditor();
					StringDictionary solutionPickerEditorAttributes = new StringDictionary();
					solutionPickerEditorAttributes.Add("UnboundReferenceType", "VSSDKAssist.Recipes.Common.References.ResxProjectItemReference, VSSDKAssist");
					solutionPickerEditor.Configure(solutionPickerEditorAttributes);
					this.veResxProjectItem.EditorInstance = solutionPickerEditor;
					this.veResxProjectItem.ConverterInstance = new ProjectItemConverter();
					this.veResxProjectItem.EndInit();

					this.veToolWindowIcon.BeginInit();
					this.veToolWindowIcon.ValueType = typeof(string);
					base.Site.Container.Add(veToolWindowIcon);
					FileChooserEditor fileChooserEditor = new FileChooserEditor();
					StringDictionary fileChooserEditorAttributes = new StringDictionary();
					fileChooserEditorAttributes.Add("Title", Properties.Resources.BitmapChooserTitle);
					fileChooserEditorAttributes.Add("Filter", Properties.Resources.BitmapChooserFilter);
					fileChooserEditor.Configure(fileChooserEditorAttributes);
					this.veToolWindowIcon.EditorInstance = fileChooserEditor;
					this.veToolWindowIcon.EndInit();
				}
			}
		}

		private void SetControls()
		{
			pctIconPreview.ImageLocation = string.Empty;
		}

		private void veToolWindowCaption_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string caption = (string)veToolWindowCaption.Value;
			DictionaryService.SetValue("ToolWindowCaption", caption);
		}

		private void veResourcesFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			ProjectItem folder = (ProjectItem)veResourcesFolder.Value;
			DictionaryService.SetValue("BitmapDestinationFolder", folder);
		}

		private void veToolWindowIcon_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string bitmapFileName = (string)veToolWindowIcon.Value;
			DictionaryService.SetValue("BitmapFileName", bitmapFileName);
			pctIconPreview.ImageLocation = bitmapFileName;
		}

		private void veResxProjectItem_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			ProjectItem resxProjectItem = (ProjectItem)veResxProjectItem.Value;
			DictionaryService.SetValue("ResxProjectItem", resxProjectItem);
		}

		private void pctToolWindowTitle_Click(object sender, EventArgs e)
		{
			Bitmap bitmap = CustomPageHelper.GetBitmapResource("ToolWindowTitle");

			if(bitmap != null)
			{
				InfoForm infoForm = new InfoForm(bitmap);

				infoForm.ShowDialog();
			}
		}
	}
}